package com.yanqu.road.entity.autodata.chuhan.data;

import com.alibaba.fastjson.JSONArray;
import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.autodata.AutoDataObject;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 阵营数据
 */
@FieldTypeAnnotation(tableName = "t_c_chuhan_camp_data")
public class CrossChuHanCampData extends AutoDataObject {

    @FieldTypeAnnotation(desc = "活动ID", select = true, update = true)
    private int activityId;

    @FieldTypeAnnotation(desc = "活动分组ID", update = true)
    private int groupId;

    @FieldTypeAnnotation(desc = "阵营ID", update = true)
    private int campId;

    @FieldTypeAnnotation(desc = "阵营红包（阵营内唯一编号,红包数据)")
    private Map<Long, CrossChuHanRedPacket> redPacketMap = new ConcurrentHashMap<>();

    @FieldTypeAnnotation(desc = "阵营红包值")
    private long redPacketValue = 0L;

    @FieldTypeAnnotation(desc = "指挥官数据")
    private CrossChuHanCommanderData commanderData;

    @FieldTypeAnnotation(desc = "每日占领过的城市")
    private Map<Integer, Set<Integer>> dayOccupyCityMap = new ConcurrentHashMap<>();

    @FieldTypeAnnotation(desc = "PVE战功cityType:zg")
    private Map<Integer, Long> pveZhanGongMap = new ConcurrentHashMap<>();
    @FieldTypeAnnotation(desc = "PVE战功变更时间cityType:time")
    private Map<Integer, Long> pveTimeMap = new ConcurrentHashMap<>();
    @FieldTypeAnnotation(desc = "获得的PVEBUFF")
    private Map<Integer, Integer> pveBuffMap = new ConcurrentHashMap<>();
    @FieldTypeAnnotation(desc = "阵营排名")
    private int rank;
    @FieldTypeAnnotation(desc = "阵营积分")
    private long rankScore;

    // ===================== 不入库 =====================

    // 玩家红包数量
    private Map<Long, CrossChuHanUserRedPacketCount> userRedPacketMap = new ConcurrentHashMap<>();

    //缓存下阵营BUFF
    private Map<Integer, CrossChuHanCampBuff> buffMap = new ConcurrentHashMap<>();

    // ==================================================

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public int getCampId() {
        return campId;
    }

    public void setCampId(int campId) {
        if (this.campId != campId) {
            this.campId = campId;
            setOp(DataStatus.Update);
        }
    }

    public Map<Long, CrossChuHanRedPacket> getRedPacketMap() {
        return redPacketMap;
    }

    public void setRedPacketMap(Map<Long, CrossChuHanRedPacket> redPacketMap) {
        if (null == redPacketMap) {
            return;
        }
        this.redPacketMap = redPacketMap;
        setOp(DataStatus.Update);
    }

    public long getRedPacketValue() {
        return redPacketValue;
    }

    public void setRedPacketValue(long redPacketValue) {
        if (this.redPacketValue != redPacketValue) {
            this.redPacketValue = redPacketValue;
            setOp(DataStatus.Update);
        }
    }

    public CrossChuHanCommanderData getCommanderData() {
        return commanderData;
    }

    public void setCommanderData(CrossChuHanCommanderData commanderData) {
        if (commanderData == null) {
            return;
        }
        this.commanderData = commanderData;
        setOp(DataStatus.Update);
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        if (this.groupId != groupId) {
            this.groupId = groupId;
            setUpdateOption();
        }
    }

    public Map<Integer, Set<Integer>> getDayOccupyCityMap() {
        return dayOccupyCityMap;
    }

    public void setDayOccupyCityMap(Map<Integer, Set<Integer>> dayOccupyCityMap) {
//        Map<Integer, Set<Integer>> tempMap = new ConcurrentHashMap<>();
//
//        for (Integer day : dayOccupyCityMap.keySet()) {
//            Object obj = dayOccupyCityMap.get(day);
//            Set<Integer> cityIds = new HashSet<>();
//
//            if (obj instanceof JSONArray) {
//                List<Integer> list = JSONArray.parseArray(obj.toString(), Integer.class);
//                cityIds.addAll(list);
//            }else {
//                cityIds.addAll((Set<Integer>) obj);
//            }
//            tempMap.put(day, cityIds);
//        }
        this.dayOccupyCityMap = dayOccupyCityMap;
        setUpdateOption();
    }

    public void addDayOccupyCity(int day, int cityId) {
        synchronized (dayOccupyCityMap) {
            Set<Integer> citySet = dayOccupyCityMap.get(day);
            if (null == citySet) {
                citySet = new HashSet<>();
                dayOccupyCityMap.put(day, citySet);
            }
            citySet.add(cityId);
        }
        setUpdateOption();
    }

    public boolean isOccupyByDay(int day, Integer cityId) {
        Set<Integer> citySet = dayOccupyCityMap.get(day);
        return citySet != null && citySet.contains(cityId);
    }

    public Map<Integer, Long> getPveZhanGongMap() {
        return pveZhanGongMap;
    }

    public void setPveZhanGongMap(Map<Integer, Long> pveZhanGongMap) {
        this.pveZhanGongMap = pveZhanGongMap;
        setUpdateOption();
    }

    public long getPveZhanGong(int cityType) {
        return pveZhanGongMap.getOrDefault(cityType, 0L);
    }

    public Map<Integer, Long> getPveTimeMap() {
        return pveTimeMap;
    }

    public void setPveTimeMap(Map<Integer, Long> pveTimeMap) {
        this.pveTimeMap = pveTimeMap;
        setUpdateOption();
    }

    public long getPveTime(int cityType) {
        return pveTimeMap.getOrDefault(cityType, 0L);
    }

    public void addPveZhanGong(int type, long zhanGong, long battleTime) {
        synchronized (pveZhanGongMap) {
            long pveZhanGong = getPveZhanGong(type) + zhanGong;
            pveZhanGongMap.put(type, pveZhanGong);
            pveTimeMap.put(type, battleTime);
        }
        setUpdateOption();
    }

    public Map<Integer, Integer> getPveBuffMap() {
        return pveBuffMap;
    }

    public void setPveBuffMap(Map<Integer, Integer> pveBuffMap) {
        this.pveBuffMap = new ConcurrentHashMap<>(pveBuffMap);
        setUpdateOption();
    }

    public int getPveBuff(int cityType) {
        return pveBuffMap.getOrDefault(cityType, 0);
    }

    public void setPveBuff(int cityType, int pveBuff) {
        pveBuffMap.put(cityType, pveBuff);
        setUpdateOption();
    }
    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
        setUpdateOption();
    }

    public long getRankScore() {
        return rankScore;
    }

    public void setRankScore(long rankScore) {
        this.rankScore = rankScore;
        setUpdateOption();
    }

    // ========= 不需要入库 =========

    public Map<Long, CrossChuHanUserRedPacketCount> getUserRedPacketMap() {
        return userRedPacketMap;
    }

    public void setUserRedPacketMap(Map<Long, CrossChuHanUserRedPacketCount> userRedPacketMap) {
        this.userRedPacketMap = userRedPacketMap;
    }

    public Map<Integer, CrossChuHanCampBuff> getBuffMap() {
        return buffMap;
    }

    public void setBuffMap(Map<Integer, CrossChuHanCampBuff> buffMap) {
        this.buffMap = buffMap;
    }
}
